package com.campus.controller;

import com.campus.pojo.dto.StudyRoomPageQueryDTO;
import com.campus.pojo.entity.StudyRoom;
import com.campus.pojo.vo.StudyRoomVO;
import com.campus.result.PageResult;
import com.campus.result.Result;
import com.campus.pojo.dto.StudyRoomDTO;
import com.campus.service.StudyRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/studyRoom")
@Slf4j
public class StudyRoomController {
    @Autowired
    private StudyRoomService studyRoomService;

    /**
     * 新增图书馆自习室
     *
     * @param studyRoomDTO
     * @return
     */
    @PostMapping
    public Result save(@RequestBody StudyRoomDTO studyRoomDTO) {
        log.info("新增图书馆自习室：{}", studyRoomDTO);
        studyRoomService.save(studyRoomDTO);
        return Result.success();
    }

    /**
     * 批量删除 前端传递ids数组 根据id批量删除 可以利用数组或者集合接收 推荐集合
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除自习室 ids：{}", ids);
        studyRoomService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 分页查询自习室 get请求 spring会将接收到的查询参数自动封装成dto对象
     * @return
     */
    @GetMapping("/pageQuery")
    public Result<PageResult<StudyRoomVO>> pageQuery(StudyRoomPageQueryDTO studyRoomPageQueryDTO) {
        log.info("自习室分页查询：{}", studyRoomPageQueryDTO);
        PageResult<StudyRoomVO> pageResult = studyRoomService.pageQuery(studyRoomPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 预约自习室-修改状态
     */
    @PutMapping("/book/{id}/{status}")
    public Result book(@PathVariable Long id, @PathVariable Integer status) {
        log.info("预约自习室id: {}  修改状态为：{}", id, status);
        studyRoomService.updateStatus(id, status);
        return Result.success();
    }

    /**
     * 查询回显
     */
    @GetMapping("/{id}")
    public Result<StudyRoom> getById(@PathVariable Long id) {
        log.info("查询回显 id: {}", id);
        StudyRoom studyRoom = studyRoomService.getById(id);
        return Result.success(studyRoom);
    }

    /**
     * 修改自习室信息
     */
    @PutMapping
    public Result update(@RequestBody StudyRoomDTO studyRoomDTO) {
        log.info("修改自习室信息：{}", studyRoomDTO);
        studyRoomService.update(studyRoomDTO);
        return Result.success();
    }

}
